package com.zzlzd.drp.controller;

import com.baidu.ueditor.ActionEnter;
import com.qiniu.storage.model.DefaultPutRet;
import com.sun.imageio.plugins.common.ImageUtil;
import com.zzlzd.drp.config.AppConfig;
import com.zzlzd.drp.config.BaseConfig;
import com.zzlzd.drp.config.ResponseInfo;
import com.zzlzd.drp.config.ResultGenerator;
import com.zzlzd.drp.util.QiniuUtils;
import lombok.Data;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.file.ConfigurationSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ceshi
 * @Title:
 * @Package
 * @Description:
 * @date 2021-3-267:29
 */
@Controller
public class UeditorController {

    @Autowired
    BaseConfig baseConfig;
    @Autowired
    ResultGenerator r;


    @ResponseBody
    @PostMapping("/uploadimage")
    public Object uploadHead(HttpServletRequest request, HttpServletResponse response,@RequestPart("upfile") MultipartFile upfile) throws IOException {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("upfile");
        String imgName = multipartFile.getOriginalFilename();
        String hz = imgName.substring(imgName.indexOf("."));
        String key = "Image_"+new Date().getTime() + ".png";
        QiniuUtils qiniuUtils = new QiniuUtils(baseConfig);
        DefaultPutRet resout = qiniuUtils.uploadFile(1,key,upfile.getInputStream());
        if(resout!=null){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("original",multipartFile.getOriginalFilename());
            map.put("name",multipartFile.getOriginalFilename());
            map.put("url",baseConfig.getUrl()+key);
            map.put("size",multipartFile.getSize());
            map.put("type","."+hz);
            map.put("state","SUCCESS");
            return map;
        }else{
            return null;
        }
    }



    @RequestMapping(value = "config")
    public JSONObject config(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            response.setContentType("application/json");
            String rootPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/ueditor/jsp";
            try {
                response.setCharacterEncoding("UTF-8");
                String exec = new ActionEnter(request, rootPath).exec();
                PrintWriter writer = response.getWriter();
                writer.write(new ActionEnter(request, rootPath).exec());
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {  //linux 和mac
            response.setHeader("Content-Type" , "text/html");
            try {
                InputStream inputStream = this.getClass().getResourceAsStream("/static/ueditor/jsp/config.json");
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer message = new StringBuffer();
                String line = null;
                while((line = br.readLine()) != null) {
                    message.append(line);
                }
                String result = message.toString().replaceAll("/\\*(.|[\\r\\n])*?\\*/","");
                JSONObject json = JSONObject.fromObject(result);
                PrintWriter out = response.getWriter();
                out.print(json.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }


    @GetMapping(value = "/ueditor")
    public String demo() {
        return "/ueditor/index";
    }
}
